package com.tripshare.sdk.weixin.api.util;

import com.tripshare.sdk.weixin.aes.AesException;
import com.tripshare.sdk.weixin.aes.WXBizMsgCrypt;
import com.tripshare.sdk.weixin.api.WeixinParameter;

/**
 * 加解密工具类
 */
public class CryptUtil {

    /**
     * 加密消息内容，消息内容是原始明文
     * @param weixinParameter
     * @return
     * @throws AesException
     */
    public static String encryptMsg(WeixinParameter weixinParameter) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(weixinParameter.getToken(), weixinParameter.getEncodingAesKey(), weixinParameter.getAppId());
        return pc.encryptMsg(weixinParameter.getContent(), weixinParameter.getTimestamp(), weixinParameter.getNonce());
    }

    /**
     * 解密微信的推送消息，消息内容是xml格式的密文消息，包含密文内容，还包含其他字段
     * @param weixinParameter
     * @return
     * @throws AesException
     */
    public static String decryptMsg(WeixinParameter weixinParameter) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(weixinParameter.getToken(), weixinParameter.getEncodingAesKey(), weixinParameter.getAppId());
        return pc.decryptMsg(weixinParameter.getMsgSignature(), weixinParameter.getTimestamp(), weixinParameter.getNonce(), weixinParameter.getContent());
    }
}
